package com.cangsan.erhai.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cangsan.erhai.common.PageBean;
import com.cangsan.erhai.common.R;
import com.cangsan.erhai.entity.TabSeek;
import com.cangsan.erhai.entity.TabVideo;
import com.cangsan.erhai.service.TabSeekService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("/seek")
public class TabSeekController {

    @Value("${upload.baseDir}")
    private  String uploadBasedir;
    @Autowired
    private TabSeekService  tabSeekService;

    @PostMapping("/list")
    public R list(@RequestBody PageBean pageBean){
        QueryWrapper<TabSeek> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("id_del",1);
        Page<TabSeek> page = new Page<>(pageBean.getPageNum(), pageBean.getPageSize());
        Page<TabSeek> pageResult = tabSeekService.page(page,queryWrapper);
        Map<String,Object> result = new HashMap<>();
        List<TabSeek> records = pageResult.getRecords();
        result.put("dataList",records);
        result.put("total",pageResult.getTotal());
        return R.ok(result);
    }

    @PostMapping("/saveOrUpdate")
    public R saveOrUpdate(@RequestBody TabSeek TabSeek){
        tabSeekService.saveOrUpdate(TabSeek);
        return R.ok();
    }


    @RequestMapping("/uploadInco")
    public Map<String,Object> uploadImage(MultipartFile file) throws IOException {
        Map<String,Object> resultMap = new HashMap<>();
        if(!file.isEmpty()){
            String originalFilename = file.getOriginalFilename();
            String suffinxName = originalFilename.substring(originalFilename.lastIndexOf("."));
            String uid = UUID.randomUUID().toString().replaceAll("-", "");
            String newFileName =uid+ DateUtil.format(new Date(),"yyyyMMdd")+suffinxName;
            FileUtils.copyInputStreamToFile(file.getInputStream(),new File(uploadBasedir+"/seek",newFileName));
            resultMap.put("code",0);
            resultMap.put("msg","上传成");
            Map<String,Object> dataMap = new HashMap<>();
            dataMap.put("title",newFileName);
            dataMap.put("src","files/seek/"+newFileName);
            resultMap.put("data",dataMap);
        }
        return resultMap;
    }
    @GetMapping("/{id}")
    public R delete(@PathVariable Long id) {
        tabSeekService.removeById(id);
        return R.ok("删除成功");
    }
    @GetMapping("/getByType/{type}")
    public R getByType(@PathVariable String type) {
        LambdaQueryWrapper<TabSeek> seekLambdaQueryWrapper = Wrappers.<TabSeek>query().lambda();
        seekLambdaQueryWrapper.eq(TabSeek::getSeekType,type);
        List<TabSeek> list = tabSeekService.list(seekLambdaQueryWrapper);
        return R.ok(list);
    }

    @GetMapping("getSeekById/{id}")
    public R getVideoById(@PathVariable Long id) {
        TabSeek byId = tabSeekService.getById(id);
        return R.ok(byId);
    }

}
